package com.ruoyi.app.controller;

import com.ruoyi.app.domain.co.user.SysUserVo;
import com.ruoyi.app.domain.vo.ShopInfoVo;
import com.ruoyi.app.domain.vo.ShopUserVo;
import com.ruoyi.app.service.IShopInfoService;
import com.ruoyi.app.service.IShopUserService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysRegisterService;
import com.ruoyi.framework.sms.SmsClient;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@Api(value = "手机端用户", tags = "用户管理接口")
@RestController
@RequestMapping("/app/userInfo")
public class AppUserInfoController extends BaseController {

    @Autowired
    private SysRegisterService sysRegisterService;

    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private SmsClient smsClient;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IShopUserService iShopUserService;

    @Autowired
    IShopInfoService shopInfoService;


    /**
     * 获取店铺列表
     */
    @ApiOperation("获取店铺列表")
    @GetMapping("/listShop")
    public AjaxResult listShop()
    {
        String userId = getUserId();
        List<ShopUserVo> shopUsers = iShopUserService.listShopUserByUserId(userId);
        return success(shopUsers);
    }

    /**
     * 切换店铺
     */
    @ApiOperation("切换店铺")
    @PostMapping("/switchShop")
    public R<String> switchShop(@RequestBody ShopUserVo shopUserVo)
    {
        if(shopUserVo.getShopId() == null){
            throw new ServiceException("店铺信息不匹配");
        }
        shopUserVo.setUserId(getUserId());
        iShopUserService.updateUserShop(shopUserVo);
        SysUser sysUser = userService.getById(shopUserVo.getUserId());
        String token = sysLoginService.getToken(sysUser);
        return R.ok(token);
    }


    /**
     * 更新用户信息
     */
    @ApiOperation("更新用户信息")
    @PostMapping("/updateUserInfo")
    public AjaxResult updateUserInfo(@RequestBody SysUserVo sysUserVo)
    {
//        if(!getUserId().equals(sysUserVo.getId())){
//            throw new ServiceException("用户信息不匹配");
//        }
        SysUser sysUser = new SysUser();
        sysUser.setId(getUserId());
        sysUser.setAvatar(sysUserVo.getAvatar());
        sysUser.setNickName(sysUserVo.getNickName());
        sysUser.setRemark(sysUserVo.getRemark());
        userService.updateById(sysUser);
        return success(true);
    }


    /**
     * 用户反馈
     */
    @ApiOperation("用户反馈")
    @PostMapping("/feedback")
    public AjaxResult feedback(@RequestBody SysUserVo sysUserVo)
    {
        return success(true);
    }


    /**
     * 用户反馈
     */
    @ApiOperation("用户反馈")
    @PostMapping("/updatePhone")
    public AjaxResult updatePhone(@RequestBody SysUserVo sysUserVo)
    {
        if(sysUserVo.getPhonenumber() == null){
            throw new ServiceException("手机号不能为空");
        }
        if(!sysUserVo.getPhonenumber().matches("^1[3456789]\\d{9}$")){
            throw new ServiceException("手机号格式错误");
        }
        SysUser sysUser = userService.getById(getUserId());
        if(sysUserVo.getPhonenumber().equals(sysUser.getPhonenumber())){
            throw new ServiceException("手机号不能与原手机号相同");
        }
        boolean b = smsClient.verifyCode(sysUserVo.getPhonenumber(), sysUserVo.getCode());
        if(!b){
            throw new ServiceException("验证码错误");
        }
        SysUser sysUser1 = new SysUser();
        sysUser1.setId(getUserId());
        sysUser1.setPhonenumber(sysUserVo.getPhonenumber());
        sysUser1.setToken(Constants.TOKEN_EXPIRED);
        userService.updateById(sysUser1);
        return success(true);
    }

    /**
     * 更新店铺信息
     */
    @ApiOperation("更新店铺信息")
    @PostMapping("/updateShopInfo")
    public AjaxResult updateShopInfo(@RequestBody ShopInfoVo shopInfoVo)
    {
        shopInfoVo.setId(getShopId());
        Boolean info = shopInfoService.updateShopInfo(shopInfoVo);
        return success(info);
    }

}
